package com.wang.generator;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MPGenerator {

    private static final String PACKAGE_NAME = "com.wang";
    private static final String MODEL_PACKAGE_NAME = "model";
    private static final String MAPPER_PACKAGE_NAME = "mapper";
    private static final String SERVICE_PACKAGE_NAME = "service";
    private static final String CONTROLLER_PACKAGE_NAME = "controller";
    private static final String MAPPER_XML_PATH = "/src/main/resources/mapper/";
    private static final String OUT_PATH = "/src/main/java";
    private static final String AUTHOR = "wang";
    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://10.0.0.29:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "huidongjia_2019";
    private static final String[] INCLUDE_TABLE_NAMES = new String[] {"order_info"};


    /**
     * 代码生成
     */
    public static void main(String[] args) {
        //1. 全局配置
        GlobalConfig config = new GlobalConfig();
        final String projectPath = System.getProperty("user.dir");

        // 是否支持AR模式
        config.setActiveRecord(false)
                .setAuthor(AUTHOR)
                // 生成路径
                .setOutputDir(projectPath + OUT_PATH)
                // 文件覆盖
                .setFileOverride(true)
                // XML 二级缓存
                .setEnableCache(false)
                // 生成的日期类型为Date
                .setDateType(DateType.ONLY_DATE)
                .setOpen(false)
                // 主键策略
                .setIdType(IdType.ASSIGN_ID)
                // 设置生成的service接口的名字的首字母不为I,注意 %s 会自动填充表实体属性
                .setServiceName("%sService")
                .setBaseResultMap(true)
                .setBaseColumnList(true);

        //2. 数据源配置
        DataSourceConfig dsConfig  = new DataSourceConfig();
        // 设置数据库类型
        dsConfig.setDbType(DbType.MYSQL)
                .setDriverName(DRIVER_NAME)
                .setUrl(URL)
                .setUsername(USERNAME)
                .setPassword(PASSWORD);

        //3. 策略配置
        StrategyConfig stConfig = new StrategyConfig();
        // 全局大写命名
        stConfig.setCapitalMode(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setRestControllerStyle(true)
                .setEntityLombokModel(true)
                .setVersionFieldName("version")
                .setLogicDeleteFieldName("is_delete")
//                .setTablePrefix("tbl_")
                // 需要生成的表
                .setInclude(INCLUDE_TABLE_NAMES);

        //4. 包名策略配置
        PackageConfig pkConfig = new PackageConfig();
        pkConfig.setParent(PACKAGE_NAME)
                .setEntity(MODEL_PACKAGE_NAME)
                .setMapper(MAPPER_PACKAGE_NAME)
                .setService(SERVICE_PACKAGE_NAME)
                .setController(null);
//                .setController(CONTROLLER_PACKAGE_NAME);
//                .setXml("mapper");


        // 注入自定义配置，可以在 VM 中使用 cfg.abc 【可无】
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-rb");
                this.setMap(map);
            }
        };
        // 调整 xml 生成目录演示
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + MAPPER_XML_PATH + tableInfo.getEntityName() + "Mapper.xml";
            }
        });
        cfg.setFileOutConfigList(focList);

        // 关闭默认 xml 生成，调整生成至根目录
        TemplateConfig tc = new TemplateConfig();
        tc.setXml(null);

        //5. 代码生成器整合配置
        AutoGenerator  ag = new AutoGenerator();

        ag.setGlobalConfig(config)
                .setDataSource(dsConfig)
                .setStrategy(stConfig)
                .setPackageInfo(pkConfig)
                .setCfg(cfg)
                .setTemplate(tc);

        //6. 执行
        ag.execute();
    }


}
